import axios from 'axios';
export default {
  namespaced: true,
state:{
  list:[]
},
mutations: {
  updateList(state,newList){
    state.list=newList;
  }
},
actions: {
  // 获取list
  async getList(context){
    try {
      const res=await axios({
        url: 'http://localhost:3000/todos',
        method:'GET'
      })
      console.log(res);
      context.commit('updateList',res.data);
    } catch (err) {
      console.log(err);
      
    }

  },
  // 删除list
  async deleteList(context,id){
    const res=await axios({
      url:` http://localhost:3000/todos/${id}`,
method:'DELETE'
    })
    console.log(res);
    context.dispatch('getList');
  },
  // 新增list
  async addList(context,newList){
    await  axios({
  url:" http://localhost:3000/todos",
method:"POST",
data:{
  name:newList.name,
  done:newList.done
}
    })
    context.dispatch('getList');
    
  },
  // 更新接口
  async patchList(context,info){
    await axios({
      url: `http://localhost:3000/todos/${info.id}`,
method:"PATCH",
data:{
  done:info.done
}
    })
  }
},
getters: {
  getList(state){
   return  state.list.filter((item)=>{
      return item.done;
    }).length

  },

}
}